package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.util.Arrays;

public class Action_Request extends AxdrType {
    private byte[] dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private ActionRequest actionRequest = null;
    private ActionRequestList actionRequestList = null;
    private ActionThenGetRequestNormalList actionThenGetRequestNormalList = null;

    public Action_Request() {
    }

    public void set_dataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setActionRequest(ActionRequest actionRequest) {
        resetChoices();
        this.choice = Choice.ACTION_REQUEST;
        this.actionRequest = actionRequest;
    }

    public void setActionRequestList(ActionRequestList actionRequestList) {
        resetChoices();
        this.choice = Choice.ACTION_REQUEST_LIST;
        this.actionRequestList = actionRequestList;
    }

    public void setActionThenGetRequestNormalList(ActionThenGetRequestNormalList actionThenGetRequestNormalList) {
        resetChoices();
        this.choice = Choice.ACTION_THEN_GET_REQUEST_NORMAL_LIST;
        this.actionThenGetRequestNormalList = actionThenGetRequestNormalList;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = Choice.getEnumByValue((int) choosen.getValue());
        if (this.choice == Choice.ACTION_REQUEST) {
            this.actionRequest = new ActionRequest();
            codeLength += this.actionRequest.decode(input);
            return codeLength;
        } else if (this.choice == Choice.ACTION_REQUEST_LIST) {
            this.actionRequestList = new ActionRequestList();
            codeLength += this.actionRequestList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.ACTION_THEN_GET_REQUEST_NORMAL_LIST) {
            this.actionThenGetRequestNormalList = new ActionThenGetRequestNormalList();
            codeLength += this.actionThenGetRequestNormalList.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (this.choice == Choice.ACTION_THEN_GET_REQUEST_NORMAL_LIST) {
            codeLength += this.actionThenGetRequestNormalList.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(3);
            codeLength += c.encode(output);
            return codeLength;
        } else if (this.choice == Choice.ACTION_REQUEST_LIST) {
            codeLength += this.actionRequestList.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(2);
            codeLength += c.encode(output);
            return codeLength;
        } else if (this.choice == Choice.ACTION_REQUEST) {
            codeLength += this.actionRequest.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(1);
            codeLength += c.encode(output);
            return codeLength;
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.actionRequest = null;
        this.actionRequestList = null;
        this.actionThenGetRequestNormalList = null;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    public String toString() {
        if (this.choice == Choice.ACTION_REQUEST) {
            if (this.actionRequest != null) {
                return "choice: {actionRequest: " + this.actionRequest + "}";
            } else {
                return "choice is actionRequest but actionRequest is null";
            }
        } else if (this.choice == Choice.ACTION_REQUEST_LIST) {
            if (this.actionRequestList != null) {
                return "choice: {actionRequestList: " + this.actionRequestList + "}";
            } else {
                return "choice is actionRequestList but actionRequestList is null";
            }
        } else if (this.choice == Choice.ACTION_THEN_GET_REQUEST_NORMAL_LIST) {
            if (this.actionThenGetRequestNormalList != null) {
                return "choice: {actionThenGetRequestNormalList: " + this.actionThenGetRequestNormalList + "}";
            } else {
                return "choice is actionThenGetRequestNormalList but actionThenGetRequestNormalList is null";
            }
        } else {
            return "unknown";
        }
    }

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        ACTION_REQUEST(1),
        ACTION_REQUEST_LIST(2),
        ACTION_THEN_GET_REQUEST_NORMAL_LIST(3);

        private final int value;

        Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}
